import "../../../../scss/activity/qrcode/qrcode_business.scss";
import $ from "jquery";
import "../../../utils/jquery.qrcode.min"
import Vue from "vue";
import html2canvas from "../../../utils/html2canvas";
import com from "../../../utils/common"
$(function () {

  let page = {
    _actid: com.getUrlParam("actid"),
    _joinid: com.getUrlParam("joinid") ?  com.getUrlParam("joinid") :0,
    _app: {},
    _pageData: {
      ActTitle: "集赞得到大礼包",
      UserName: "111",
      BannerImg: "/img/activity/cut_price/base_cut/banner_image_skin01.png",
    },
    _init: function () {
      let self = this;
      com.ajax({
        url: "/normal/getQrcodeInfo",
        data: {
          Actid: self._actid,
          Joinid:self._joinid,
        },
        ok: function (data) {
          let temp = data["msg"];
          if(temp && temp!="fail"){
            console.log(data);
            $.extend(self._pageData , temp);
            self._loadTemp()
          }else{
            history.go(-1);
          }
        }
      })
    },
    _loadTemp: function () {
      let self = this;
      if(!self._actid || self._actid == "" || self._actid<=0){
        history.go(-1);
      }
      self._app = new Vue({
        el: "#app",
        data: self._pageData,
        methods: {
          GoBack: function () {
            history.go(-1);
          }
        }
      })
      let width = document.documentElement.clientWidth;
      let nowScale = width / 750;
      $('#code_img').qrcode({
        correctLevel: 0,
        width: 300 * nowScale,
        height: 300 * nowScale,
        text:self._pageData.url,  // 二维码数据
      });
      self._afterTemp();
    },
    _afterTemp: function () {
      let main = {
        //获取像素密度
        getPixelRatio: function (context) {
          let backingStore =
            context.backingStorePixelRatio ||
            context.webkitBackingStorePixelRatio ||
            context.mozBackingStorePixelRatio ||
            context.msBackingStorePixelRatio ||
            context.oBackingStorePixelRatio ||
            context.backingStorePixelRatio ||
            1;
          return (window.devicePixelRatio || 1) / backingStore;
        },

        saveImg: function (domImage, imgPosition) {
          let shareContent = domImage; // 需要绘制的部分的 (原生）dom 对象 ，注意容器的宽度不要使用百分比，使用固定宽度，避免缩放问题
          let width = shareContent.width(); // 获取(原生）dom 宽度
          let height = shareContent.height(); // 获取(原生）dom 高
          let offsetTop = shareContent.offset().top; //元素距离顶部的偏移量
          //console.log(offsetTop);
          let canvas = document.createElement("canvas"); //创建canvas 对象
          let context = canvas.getContext("2d");
          let scaleBy = main.getPixelRatio(context); //获取像素密度的方法 (也可以采用自定义缩放比例)
          //console.log(scaleBy);
          canvas.width = width * scaleBy; //这里 由于绘制的dom 为固定宽度，居中，所以没有偏移
          canvas.height = (height + offsetTop) * scaleBy; // 注意高度问题，由于顶部有个距离所以要加上顶部的距离，解决图像高度偏移问题
          context.scale(scaleBy, scaleBy);
          html2canvas(domImage, {
            onrendered: function (canvas) {
              let url = canvas.toDataURL();
              let img = new Image();
              img.onload = function () {
              };
              img.src = url;
              domImage.hide();
              imgPosition.append(img);
            }
          });
        }
      };
      setTimeout(function () {
        main.saveImg($(".share-container"), $(".container"));
      }, 0);

    }
  };
  page._init()
});

